package com.fulihui.systemcore.service;

import com.fulihui.information.api.GoodFeedbackService;
import com.fulihui.information.request.GoodFeedbackCreateRequest;
import com.fulihui.systemcore.api.VisitRecordService;
import com.fulihui.systemcore.dto.VisitRecordDTO;
import com.fulihui.systemcore.repository.VisitRecordRepository;
import com.fulihui.systemcore.request.callcenter.VisitRecordCreateRequest;
import com.fulihui.systemcore.request.callcenter.VisitRecordQueryRequest;
import com.fulihui.systemcore.request.callcenter.VisitRecordUpdateRequest;
import org.near.servicesupport.request.TPageRequest;
import org.near.servicesupport.request.TRequest;
import org.near.servicesupport.result.*;
import org.near.servicesupport.util.ConditionValidator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.inject.Inject;
import java.util.List;

/**
 * @author leeson on 2016/6/6.
 */
@Service("visitRecordService")
public class VisitRecordServiceImpl implements VisitRecordService {
    @Autowired
    private VisitRecordRepository visitRecordRepository;
    @Resource
    private GoodFeedbackService goodFeedbackService;
    @Override
    public TSingleResult<VisitRecordDTO> querySingle(TRequest<Long> request) {
        Integer id = request.getCondition().intValue();
        return ResultBuilder.succTSingle(visitRecordRepository.querySingle(id));
    }

    @Override
    public BaseResult create(TRequest<VisitRecordCreateRequest> request) {
        VisitRecordCreateRequest condition = request.getCondition();
        ConditionValidator.validate(condition);
        //// FIXME: 2017/5/18 为了不让前端发包，临时方案，VisitRecordService#create有益反馈,直接调用information中的服务
        GoodFeedbackCreateRequest goodFeedbackCreateRequest = new GoodFeedbackCreateRequest();
        BeanUtils.copyProperties(condition, goodFeedbackCreateRequest);
        goodFeedbackService.create(goodFeedbackCreateRequest);
//        VisitRecordDTO record = new VisitRecordDTO();
//        BeanUtils.copyProperties(condition, record);
//
//        visitRecordRepository.insert(record);

        return ResultBuilder.succ(BaseResult.class);
    }

    @Override
    public BaseResult update(TRequest<VisitRecordUpdateRequest> request) {
        VisitRecordUpdateRequest condition = request.getCondition();
        ConditionValidator.validate(condition);

        VisitRecordDTO record = new VisitRecordDTO();
        BeanUtils.copyProperties(condition, record);
        record.setId(condition.getRecordId());

        visitRecordRepository.update(record);

        return ResultBuilder.succ(BaseResult.class);
    }

    @Override
    public TPageResult<VisitRecordDTO> queryPage(TPageRequest<VisitRecordQueryRequest> request) {
        VisitRecordQueryRequest condition = request.getCondition();

        List<VisitRecordDTO> visitRecordList = visitRecordRepository.queryPage(
                condition.getNickName(), condition.getPhone(), condition.getStartTime(),
                condition.getEndTime(), request.start4Mysql(), request.getRows());

        int totalCount = visitRecordRepository.queryPageCount(condition.getNickName(),
                condition.getPhone(), condition.getStartTime(), condition.getEndTime());

        return ResultBuilder.succTPage(visitRecordList, request.getPage(), request.getRows(),
                totalCount);
    }

    @Override
    public TMultiResult<VisitRecordDTO> queryAll(TRequest<VisitRecordQueryRequest> request) {
        VisitRecordQueryRequest condition = request.getCondition();

        List<VisitRecordDTO> visitRecordList = visitRecordRepository.queryAll(
                condition.getNickName(), condition.getPhone(), condition.getStartTime(),
                condition.getEndTime());

        return ResultBuilder.succTMulti(visitRecordList);
    }
}
